import time

#requires settings.email*
while True:
    rows = db(db.queue.status=='pending').select(db.queue.ALL, limitby=(0, settings.email_queue_max))
    for row in rows:
        if mail.send(to=row.email,
            subject=row.subject,
            message=row.message,
            bcc=settings.email_bcc):
            row.update_record(status='sent')
        else:
            row.update_record(status='failed')
        db.commit()
    time.sleep(settings.email_queue_interval)